package model;
import tools.*;
import java.io.*;
import java.util.*;
//flight.txt文件中储存了所有”订单号“对应的”航班编号“以及“具体事项（选择的食物）”（每一行代表一个订单，例如：1326854870744 BA0570 FOODUNCHOSEN）
public class flight {
    public static String findFlight_line(String content){
        lineReader lr = new lineReader();
        String flight_number_line = lr.readLine("resource/flight.txt",content);
        return flight_number_line;
    }
//findFlight函数，能根据订单号找到对应的航班编号，括号内参数（content）应为订单号，返回值（flight_number）是该订单号对应的航班编号
    public static String findFlight(String content){
        String temp = findFlight_line(content);
        String[] sp = temp.split("\\s+");
        String flight_number = sp[1];
        return flight_number;
    }
//add_food函数，括号中参数添加正在选择事项的”订单号（order_number）“以及所选择的”具体事项（food）“
//添加成功后，每一行订单号最后代表事项未选择的"FOODUNCHOSEN"会被替换成所添加的（String food）
    public static void add_food(String order_number,String food){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        int lineNum=lr.findLine("resource/flight.txt",order_number);
        String reLine = lw.lineRewrite("resource/flight.txt",lineNum,"FOODUNCHOSEN",food);
        lw.writeLine("resource/flight.txt",lineNum,reLine);
    }

    //add_seat函数，括号中参数添加正在选择事项的”订单号（order_number）“以及所选择的”座位排数（letter）““座位列数（number）”
//添加成功后，每一行订单号最后代表事项未选择的"LETTERUNCHOSEN""NUMBERUNCHOSEN"会被替换成所添加的（String letter,String number)
    public static void add_seat(String order_number,String letter,String number){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        int lineNum=lr.findLine("resource/flight.txt",order_number);
        String reLine1 = lw.lineRewrite("resource/flight.txt",lineNum,"LETTERUNCHOSEN",letter);
        lw.writeLine("resource/flight.txt",lineNum,reLine1);
        String reLine2 = lw.lineRewrite("resource/flight.txt",lineNum,"NUMBERUNCHOSEN",number);
        lw.writeLine("resource/flight.txt",lineNum,reLine2);
    }

    public static void add_note(String order_number,String note){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        int lineNum=lr.findLine("resource/flight.txt",order_number);
        String reLine = lw.lineRewrite("resource/flight.txt",lineNum,"NOTE",note);
        lw.writeLine("resource/flight.txt",lineNum,reLine);
    }

    //boarding函数，括号中参数添加正在选择事项的”订单号（order_number）“
//添加成功后，每一行订单号最后代表事项未选择的"UNBOARDING"会被替换成所添加的"ISBOARDING"
    public static void boarding(String order_number){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        int lineNum=lr.findLine("resource/flight.txt",order_number);
        String reLine = lw.lineRewrite("resource/flight.txt",lineNum,"UNBOARDING","ISBOARDING");
        lw.writeLine("resource/flight.txt",lineNum,reLine);
    }

    public static boolean isBoarding(String flight_number,String id_number){
        lineReader lr = new lineReader();
        lineWriter lw = new lineWriter();
        String judgement=null;
        try{
            FileReader fr =new FileReader(new File("resource/flight.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line!=null){
                if(line.contains(flight_number)){
                    if(line.contains(id_number)){
                        break;
                    }else {
                        line=br.readLine();
                    }
                }else{
                    line=br.readLine();
                }
            }
            judgement = line;
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        String str="ISBOARDING";
        return judgement.contains(str);
    }


    public  static  String  find_all_flights(){
        String result=null;
        try{
            FileReader fr =new FileReader(new File("resource/flight.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            String[] sp = line.split("\\s+");
            result=sp[1];
            while(line!=null){
                sp=line.split("\\s+");
                result=result+" "+sp[1];
                line = br.readLine();
            }

        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }
    //isFlight方法能够判断输入的号码是否为已有的航班号吗 ，括号中参数为想要判断的字符串，如果有该航班号码则返回int类型的数值1，否则返回0
    public  static int isFlight(String content){
        int judgement=0;
        String[] sp = find_all_flights().split("\\s+");
        int i=0;
        while(i<sp.length){
            if(sp[i].equals(content)){
                judgement=1;
                break;
            }
            i=i+1;
        }
        return judgement;
    }

    public static ArrayList<String> administer_findEachFlight_Information(String flight_num){
        ArrayList<String> result=new ArrayList<>();
        try{
            FileReader fr =new FileReader(new File("resource/flight.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line!=null){
                if(line.contains(flight_num)){
                    result.add(line);
                    line= br.readLine();
                }else{
                    line=br.readLine();
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    public static int administer_findEachFlight_Num(String flight_num){
        int result=0;
        try{
            FileReader fr =new FileReader(new File("resource/flight.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line!=null){
                if(line.contains(flight_num)){
                    line= br.readLine();
                    result++;
                }else{
                    line=br.readLine();
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    public static int IS_VIP(String order_num){
        int result=0;
        try{
            FileReader fr =new FileReader(new File("resource/flight.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            String[] temp =null;
            while(line!=null){
                if(line.contains(order_num)){
                    if(line.contains("VIP")){
                        result=1;
                    }
                    break;
                }else{
                    line = br.readLine();
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }
}
